package hot100.logos31To40;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author logos
 * date 2025/7/13 12:47
 * @version v1.0
 * @Package hot100.logos31To40
 */
public class logos35 {

    /**
     * 35. LRU缓存
     * https://leetcode.cn/problems/lru-cache/?envType=study-plan-v2&envId=top-100-liked
     */

    class LRUCache {

        class DoubleNode {
            int val;
            int key;
            DoubleNode pre;
            DoubleNode next;

            DoubleNode(int key, int val) {
                this.key = key;
                this.val = val;
                pre = null;
                next = null;
            }
        }

        class DoubleList {
            DoubleNode head;
            DoubleNode tail;

            DoubleList() {
                head = null;
                tail = null;
            }

            public void addNode(DoubleNode node) {
                if (node == null) {
                    return;
                }
                if (head == null) {
                    head = node;
                    tail = node;
                } else {
                    tail.next = node;
                    node.pre = tail;
                    tail = node;
                }
            }

            public DoubleNode removeHeadNode() {
                if (head == null) {
                    return null;
                }
                DoubleNode ans = head;
                if (head == tail) {
                    head = null;
                    tail = null;
                } else {
                    head = ans.next;
                    head.pre = null;
                    ans.next = null;
                }
                return ans;
            }

            public void moveNodeToTail(DoubleNode node) {
                if (node == tail) {
                    return;
                }
                if (node == head) {
                    head = head.next;
                    head.pre = null;
                } else {
                    node.pre.next = node.next;
                    node.next.pre = node.pre;
                }
                node.next = null;
                tail.next = node;
                node.pre = tail;
                tail = node;
            }
        }

        public DoubleList doubleList;

        public HashMap<Integer, DoubleNode> map = new HashMap<>();

        public int capacity;

        public LRUCache(int capacity) {
            doubleList = new DoubleList();
            this.capacity = capacity;
        }

        public int get(int key) {
            if (map.containsKey(key)) {
                DoubleNode node = map.get(key);
                doubleList.moveNodeToTail(node);
                return node.val;
            }
            return -1;
        }

        public void put(int key, int value) {
            if (map.containsKey(key)) {
                DoubleNode node = map.get(key);
                node.val = value;
                doubleList.moveNodeToTail(node);
            } else {
                if (capacity == map.size()) {
                    DoubleNode node = doubleList.removeHeadNode();
                    map.remove(node.key);
                }
                DoubleNode node = new DoubleNode(key, value);
                doubleList.addNode(node);
                map.put(key, node);
            }
        }
    }

    class LRUCache2 {
        private final int capacity;
        private final Map<Integer, Integer> map = new LinkedHashMap<>();

        public LRUCache2(int capacity) {
            this.capacity = capacity;
        }

        public int get(int key) {
            Integer value = map.remove(key);
            if (value != null) {
                map.put(key, value);
                return value;
            }
            return -1;
        }

        public void put(int key, int value) {
            if (map.remove(key) != null) {
                map.put(key, value);
                return;
            }
            if (map.size() == capacity) {
                Integer eldestKey = map.keySet().iterator().next();
                map.remove(eldestKey);
            }
            map.put(key, value);
        }
    }

}
